﻿unit SmSendMstModel;

interface

uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm;

type

  TSmSendMstModel = class(TObject)
  public 
    const cModuleKey='SmSendMst'; 
    const cTblName ='sm_send_mst';
    const cBaseView = 'view_sm_send_mst';
    const cObjNameKey='ObjName';
    const cObjGUIDKey='ObjGUID';
    const cObjItemsKey='Items';
    const cCdsModelKey='CdsModel';
    
    class function GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetQuerySQL(const iCondition:string; const dOrderBy:string=''; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;
    class function GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode=eDBLoadMode.dblmTable):string;

    const cSendIdKey='send_id';
    const cSendBillTypeKey='send_bill_type';
    const cCustomerIdKey='customer_id';
    const cSendStatusKey='send_status';
    const cSendRemarkKey='send_remark';
    const cOrgIdKey='org_id';
    const cRecordPsnKey='record_psn';
    const cRecordDateKey='record_date';
    const cModifyPsnKey='modify_psn';
    const cModifyDateKey='modify_date';
    const cApprovePsnKey='approve_psn';
    const cApproveDateKey='approve_date';
    const cCancelPsnKey='cancel_psn';
    const cCancelDateKey='cancel_date';
    const cTimeStampKey='time_stamp';
  end;
  
  TSmSendMst = class(TObject)
  private    
    fSendId:string;
    fSendBillType:Integer;
    fCustomerId:string;
    fSendStatus:Integer;
    fSendRemark:string;
    fOrgId:string;
    fRecordPsn:string;
    fRecordDate:TDateTime;
    fModifyPsn:string;
    fModifyDate:TDateTime;
    fApprovePsn:string;
    fApproveDate:TDateTime;
    fCancelPsn:string;
    fCancelDate:TDateTime;
    fTimeStamp:Int64;
    
  protected
    fObjGUID:string;
    fInitDate:TDateTime;
    fCdsModel:TClientDataSet;
    function ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    property ObjGUID:string read fObjGUID;
    property SendId:string read fSendId write fSendId;
    property SendBillType:Integer read fSendBillType write fSendBillType;
    property CustomerId:string read fCustomerId write fCustomerId;
    property SendStatus:Integer read fSendStatus write fSendStatus;
    property SendRemark:string read fSendRemark write fSendRemark;
    property OrgId:string read fOrgId write fOrgId;
    property RecordPsn:string read fRecordPsn write fRecordPsn;
    property RecordDate:TDateTime read fRecordDate write fRecordDate;
    property ModifyPsn:string read fModifyPsn write fModifyPsn;
    property ModifyDate:TDateTime read fModifyDate write fModifyDate;
    property ApprovePsn:string read fApprovePsn write fApprovePsn;
    property ApproveDate:TDateTime read fApproveDate write fApproveDate;
    property CancelPsn:string read fCancelPsn write fCancelPsn;
    property CancelDate:TDateTime read fCancelDate write fCancelDate;
    property TimeStamp:Int64 read fTimeStamp write fTimeStamp;
  end;

  TSmSendMstList = class(TDictionary<string,TSmSendMst>)
  private
    fCdsModel:TClientDataSet;
    fSortKeys:TStrings;
    
  public
    constructor Create();overload;
    destructor Destroy;override;
    
    procedure ResetObject;
    
    function LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
    function LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
    function SaveToStr():string;
    
    function FindByKey(const iKey:string):TSmSendMst;
    function IsExist(iKey:string):Boolean;
    function Add(const iItem:TSmSendMst):string;
    function Delete(const iKey:string):Boolean;
    function Remove(const iKey:string):TSmSendMst;
    procedure Clear;
    
    property SortKeys:TStrings read fSortKeys write fSortKeys;
  end;

implementation

class function TSmSendMstModel.GetCountSQL(const iCondition:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmSendMstModel.cTblName;
    dblmTable: ReadName:=TSmSendMstModel.cTblName;
    dblmView: ReadName:=TSmSendMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s;'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT COUNT(*) AS ''COUNT'' FROM %s WHERE %s;'),[ReadName,iCondition]);
  end;
end;

class function TSmSendMstModel.GetQuerySQL(const iCondition:string; const dOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmSendMstModel.cTblName;
    dblmTable: ReadName:=TSmSendMstModel.cTblName;
    dblmView: ReadName:=TSmSendMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin
    Result:=Format(string('SELECT * FROM %s %s;'),[ReadName,
                                                 IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s %s;'),[ReadName,iCondition,
                                                            IfThen(SameStr(dOrderBy,EmptyStr),'','ORDER BY '+dOrderBy)]);
  end;
end;

class function TSmSendMstModel.GetPageQuerySQL(const iPageNo,iPageSize:Integer; const iCondition,iOrderBy:string; const dLoadMode:eDBLoadMode):string;
var
  ReadName:string;
begin
  case dLoadMode of
    dblmNone: ReadName:=TSmSendMstModel.cTblName;
    dblmTable: ReadName:=TSmSendMstModel.cTblName;
    dblmView: ReadName:=TSmSendMstModel.cBaseView;
  end;
  
  if SameStr(Trim(iCondition),EmptyStr) then
  begin      
    Result:=Format(string('SELECT * FROM %s'),[ReadName]);
  end else begin
    Result:=Format(string('SELECT * FROM %s WHERE %s'),[ReadName,iCondition]);
  end;
  Result:=Format(string('SELECT TOP %d * FROM (SELECT row_number() OVER(ORDER BY %s) AS _row_no_,* FROM (%s) AS A) AS B WHERE _row_no_>%d ORDER BY _row_no_'),
                  [iPageSize,
                  IfThen(SameStr(Trim(iOrderBy),EmptyStr),'',iOrderBy),
                  Result,
                  (iPageNo-1)*iPageSize]);
end;

constructor TSmSendMst.Create();
begin
  inherited Create;
  fInitDate:=EncodeDateTime( 1899, 12, 30, 0, 0, 0, 0 );
  fCdsModel:=TClientDataSet.Create(nil);
  ResetObject;
end;

destructor TSmSendMst.Destroy;
begin
  FreeAndNil(fCdsModel);
  inherited Destroy;
end;

procedure TSmSendMst.ResetObject;
begin
  fObjGUID:=TSK0001Comm.GetGUID;
  fSendId:=EmptyStr;
  fSendBillType:=-1;
  fCustomerId:=EmptyStr;
  fSendStatus:=-1;
  fSendRemark:=EmptyStr;
  fOrgId:=EmptyStr;
  fRecordPsn:=EmptyStr;
  fRecordDate:=fInitDate;
  fModifyPsn:=EmptyStr;
  fModifyDate:=fInitDate;
  fApprovePsn:=EmptyStr;
  fApproveDate:=fInitDate;
  fCancelPsn:=EmptyStr;
  fCancelDate:=fInitDate;
  fTimeStamp:=-1;
end;

function TSmSendMst.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    ResetObject;
    if (iCds.FindField(TSmSendMstModel.cSendIdKey)<>nil) then
    begin
      fSendId:=iCds.FieldByName(TSmSendMstModel.cSendIdKey).AsString;     
      fObjGUID:=TSK0001Comm.SetObjGUID(fSendId);
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendBillTypeKey)<>nil) then
    begin
      fSendBillType:=iCds.FieldByName(TSmSendMstModel.cSendBillTypeKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendBillTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCustomerIdKey)<>nil) then
    begin
      fCustomerId:=iCds.FieldByName(TSmSendMstModel.cCustomerIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendStatusKey)<>nil) then
    begin
      fSendStatus:=iCds.FieldByName(TSmSendMstModel.cSendStatusKey).AsInteger;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendRemarkKey)<>nil) then
    begin
      fSendRemark:=iCds.FieldByName(TSmSendMstModel.cSendRemarkKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cOrgIdKey)<>nil) then
    begin
      fOrgId:=iCds.FieldByName(TSmSendMstModel.cOrgIdKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cRecordPsnKey)<>nil) then
    begin
      fRecordPsn:=iCds.FieldByName(TSmSendMstModel.cRecordPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cRecordDateKey)<>nil) then
    begin
      if iCds.FindField(TSmSendMstModel.cRecordDateKey).IsNull then
      begin
        fRecordDate:=fInitDate;
      end else begin
        fRecordDate:=iCds.FieldByName(TSmSendMstModel.cRecordDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cModifyPsnKey)<>nil) then
    begin
      fModifyPsn:=iCds.FieldByName(TSmSendMstModel.cModifyPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cModifyDateKey)<>nil) then
    begin
      if iCds.FindField(TSmSendMstModel.cModifyDateKey).IsNull then
      begin
        fModifyDate:=fInitDate;
      end else begin
        fModifyDate:=iCds.FieldByName(TSmSendMstModel.cModifyDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cApprovePsnKey)<>nil) then
    begin
      fApprovePsn:=iCds.FieldByName(TSmSendMstModel.cApprovePsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cApprovePsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cApproveDateKey)<>nil) then
    begin
      if iCds.FindField(TSmSendMstModel.cApproveDateKey).IsNull then
      begin
        fApproveDate:=fInitDate;
      end else begin
        fApproveDate:=iCds.FieldByName(TSmSendMstModel.cApproveDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cApproveDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCancelPsnKey)<>nil) then
    begin
      fCancelPsn:=iCds.FieldByName(TSmSendMstModel.cCancelPsnKey).AsString;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCancelPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCancelDateKey)<>nil) then
    begin
      if iCds.FindField(TSmSendMstModel.cCancelDateKey).IsNull then
      begin
        fCancelDate:=fInitDate;
      end else begin
        fCancelDate:=iCds.FieldByName(TSmSendMstModel.cCancelDateKey).AsDateTime;
      end;    
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCancelDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cTimeStampKey)<>nil) then
    begin
      fTimeStamp:=iCds.FieldByName(TSmSendMstModel.cTimeStampKey).AsLargeInt;     
    end else begin
      oError:=Format(string('%s装载错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmSendMst.ObjectToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    if (iCds.FindField(TSmSendMstModel.cSendIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cSendIdKey).AsString:=fSendId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendBillTypeKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cSendBillTypeKey).AsInteger:=fSendBillType;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendBillTypeKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCustomerIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cCustomerIdKey).AsString:=fCustomerId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCustomerIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendStatusKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cSendStatusKey).AsInteger:=fSendStatus;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendStatusKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cSendRemarkKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cSendRemarkKey).AsString:=fSendRemark;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cSendRemarkKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cOrgIdKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cOrgIdKey).AsString:=fOrgId;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cOrgIdKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cRecordPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cRecordPsnKey).AsString:=fRecordPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cRecordPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cRecordDateKey)<>nil) then
    begin
      if SameDateTime(fRecordDate,fInitDate) then
      begin
        iCds.FieldByName(TSmSendMstModel.cRecordDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmSendMstModel.cRecordDateKey).AsDateTime:=fRecordDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cRecordDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cModifyPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cModifyPsnKey).AsString:=fModifyPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cModifyPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cModifyDateKey)<>nil) then
    begin
      if SameDateTime(fModifyDate,fInitDate) then
      begin
        iCds.FieldByName(TSmSendMstModel.cModifyDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmSendMstModel.cModifyDateKey).AsDateTime:=fModifyDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cModifyDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cApprovePsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cApprovePsnKey).AsString:=fApprovePsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cApprovePsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cApproveDateKey)<>nil) then
    begin
      if SameDateTime(fApproveDate,fInitDate) then
      begin
        iCds.FieldByName(TSmSendMstModel.cApproveDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmSendMstModel.cApproveDateKey).AsDateTime:=fApproveDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cApproveDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCancelPsnKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cCancelPsnKey).AsString:=fCancelPsn;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCancelPsnKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cCancelDateKey)<>nil) then
    begin
      if SameDateTime(fCancelDate,fInitDate) then
      begin
        iCds.FieldByName(TSmSendMstModel.cCancelDateKey).AsString:=EmptyStr;
      end else begin
        iCds.FieldByName(TSmSendMstModel.cCancelDateKey).AsDateTime:=fCancelDate;
      end;    
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cCancelDateKey]);
      Result:=False;
      Exit;
    end;
    if (iCds.FindField(TSmSendMstModel.cTimeStampKey)<>nil) then
    begin
      iCds.FieldByName(TSmSendMstModel.cTimeStampKey).AsLargeInt:=fTimeStamp;     
    end else begin
      oError:=Format(string('%s对象转数据集错误:数据集中缺少%s字段!'),[Self.ClassName,TSmSendMstModel.cTimeStampKey]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.ObjectToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmSendMst.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
begin
  if (iCds<>nil) then
  begin
    iCds.Append;
    if ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误!错误内容:传入的Cds对象为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmSendMst.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmSendMstModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmSendMstModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数！错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性节点.'),[Self.ClassName,TSmSendMstModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    ResetObject;
      
    if (Json.ItemByName(TSmSendMstModel.cCdsModelKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmSendMstModel.cCdsModelKey).AsString),EmptyStr) then
    begin
      TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmSendMstModel.cCdsModelKey).AsString),MemStream);
      if MemStream.Size>0 then
      begin
        fCdsModel.LoadFromStream(MemStream);
      end;
    end;
    
    if (Json.ItemByName(TSmSendMstModel.cObjGUIDKey)<>nil) and 
      not SameStr(Trim(Json.ItemByName(TSmSendMstModel.cObjGUIDKey).AsString),EmptyStr) then
    begin
      fObjGUID:=Json.ItemByName(TSmSendMstModel.cObjGUIDKey).AsString;
    end;
    
    if Json.ItemByName('SendId')<>nil then
    begin
      fSendId:=Json.ItemByName('SendId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'SendId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('SendBillType')<>nil then
    begin
      fSendBillType:=Json.ItemByName('SendBillType').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'SendBillType']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CustomerId')<>nil then
    begin
      fCustomerId:=Json.ItemByName('CustomerId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CustomerId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('SendStatus')<>nil then
    begin
      fSendStatus:=Json.ItemByName('SendStatus').AsInteger;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'SendStatus']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('SendRemark')<>nil then
    begin
      fSendRemark:=Json.ItemByName('SendRemark').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'SendRemark']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('OrgId')<>nil then
    begin
      fOrgId:=Json.ItemByName('OrgId').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'OrgId']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordPsn')<>nil then
    begin
      fRecordPsn:=Json.ItemByName('RecordPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('RecordDate')<>nil then
    begin
      fRecordDate:=Json.ItemByName('RecordDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'RecordDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyPsn')<>nil then
    begin
      fModifyPsn:=Json.ItemByName('ModifyPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ModifyDate')<>nil then
    begin
      fModifyDate:=Json.ItemByName('ModifyDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ModifyDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ApprovePsn')<>nil then
    begin
      fApprovePsn:=Json.ItemByName('ApprovePsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ApprovePsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('ApproveDate')<>nil then
    begin
      fApproveDate:=Json.ItemByName('ApproveDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'ApproveDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CancelPsn')<>nil then
    begin
      fCancelPsn:=Json.ItemByName('CancelPsn').AsString;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CancelPsn']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('CancelDate')<>nil then
    begin
      fCancelDate:=Json.ItemByName('CancelDate').AsDateTime;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'CancelDate']);
      Result:=False;
      Exit;  
    end;
    if Json.ItemByName('TimeStamp')<>nil then
    begin
      fTimeStamp:=Json.ItemByName('TimeStamp').AsInt64;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数！错误内容:缺少%s属性.'),[Self.ClassName,'TimeStamp']);
      Result:=False;
      Exit;  
    end;
    
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmSendMst.SaveToStr():string;
var
  Json:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;  
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create; 
  try
    Json.Add('ModuleKey').AsString:=TSmSendMstModel.cModuleKey;
    Json.Add(TSmSendMstModel.cObjNameKey).AsString:=Self.ClassName;
    
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmSendMstModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));
    
    Json.Add(TSmSendMstModel.cObjGUIDKey).AsString:=fObjGUID;
    Json.Add('SendId').AsString:=fSendId;
    Json.Add('SendBillType').AsInteger:=fSendBillType;
    Json.Add('CustomerId').AsString:=fCustomerId;
    Json.Add('SendStatus').AsInteger:=fSendStatus;
    Json.Add('SendRemark').AsString:=fSendRemark;
    Json.Add('OrgId').AsString:=fOrgId;
    Json.Add('RecordPsn').AsString:=fRecordPsn;
    Json.Add('RecordDate').AsDateTime:=fRecordDate;
    Json.Add('ModifyPsn').AsString:=fModifyPsn;
    Json.Add('ModifyDate').AsDateTime:=fModifyDate;
    Json.Add('ApprovePsn').AsString:=fApprovePsn;
    Json.Add('ApproveDate').AsDateTime:=fApproveDate;
    Json.Add('CancelPsn').AsString:=fCancelPsn;
    Json.Add('CancelDate').AsDateTime:=fCancelDate;
    Json.Add('TimeStamp').AsInt64:=fTimeStamp;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

constructor TSmSendMstList.Create();
begin
  inherited Create;
  fCdsModel:=TClientDataSet.Create(nil);
  fSortKeys:=TStringList.Create;
  ResetObject;
end;

destructor TSmSendMstList.Destroy;
begin
  FreeAndNil(fCdsModel);
  Clear;
  FreeAndNil(fSortKeys);
  inherited Destroy;
end;

procedure TSmSendMstList.ResetObject;
begin
  Clear;
end;

function TSmSendMstList.LoadFromCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Item:TSmSendMst;
begin
  ResetObject;
  if iCds<>nil then
  begin
    iCds.First;
    while not iCds.Eof do
    begin
      Item:=TSmSendMst.Create;
      if Item.LoadFromCDS(iCds,oError) then
      begin
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end else begin
        Result:=False;
        Exit;
      end;
      iCds.Next;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadFromCDS函数参数错误! 错误内容传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmSendMstList.SaveToCDS(const iCds:TClientDataSet; out oError:string):Boolean;
var
  Key:string;
begin
  if (iCds<>nil) then
  begin
  for Key in fSortKeys do
  begin
    iCds.Append;
    if Items[Key].ObjectToCDS(iCds,oError) then
    begin
      iCds.Post;
    end else begin
      Result:=False;
      Exit;
    end;
  end;
  end else begin
    oError:=Format(string('调用%s.SaveToCDS函数参数错误! 错误内容:传入的数据集为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSmSendMstList.LoadFromStr(const iJsonStr:string; out oError:string):Boolean;
var
  Json,ItemJson,ArrayJson:TQJson;
  Item:TSmSendMst;
  MemStream:TMemoryStream;
begin
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    if Json.TryParse(iJsonStr) then
    begin
      if (Json.ItemByName(TSmSendMstModel.cObjNameKey)<>nil) then
      begin
        if (Self.ClassName<>Json.ItemByName(TSmSendMstModel.cObjNameKey).AsString) then
        begin
          //oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:对象类名不匹配.'),[Self.ClassName]);
          //Result:=False;
          //Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:缺少%s属性节点.'),[Self.ClassName,TSmSendMstModel.cObjNameKey]);
        Result:=False;
        Exit;
      end;
    end else begin
      oError:=Format(string('调用%s.LoadFromStr函数参数错误! 错误内容:解析Json失败.iJsonStr:%s'),[Self.ClassName,iJsonStr]);
      Result:=False;
      Exit;
    end;
    if (Json.ItemByName(TSmSendMstModel.cObjItemsKey)<>nil) and
      (Json.ItemByName(TSmSendMstModel.cObjItemsKey).IsArray) then
    begin
      ResetObject;
      if Json.ItemByName(TSmSendMstModel.cCdsModelKey)<>nil then
      begin
        TSK0001Comm.Base64StrToMemStream(AnsiString(Json.ItemByName(TSmSendMstModel.cCdsModelKey).AsString),MemStream);
        if MemStream.Size>0 then
        begin
          fCdsModel.LoadFromStream(MemStream);
        end;
      end;
      ArrayJson:=Json.ItemByName(TSmSendMstModel.cObjItemsKey);
      for ItemJson in ArrayJson do
      begin
        Item:=TSmSendMst.Create;
        Item.LoadFromStr(ItemJson.AsString,oError);
        if Add(Item)=EmptyStr then
        begin
          Item.Free;
          Result:=False;
          Exit;
        end;
      end;
    end else begin
      Result:=False;
      Exit;
    end;
  finally
    Json.Free;
    MemStream.Free;
  end;
  Result:=True;
end;

function TSmSendMstList.SaveToStr():string;
var
  Key:string;
  Json,ItemJson,ArrayJson:TQJson;
  MemStream:TMemoryStream;
begin
  Result:=EmptyStr;
  Json:=TQJson.Create;
  MemStream:=TMemoryStream.Create;
  try
    Json.Add(TSmSendMstModel.cObjNameKey).AsString:=Self.ClassName;
    fCdsModel.SaveToStream(MemStream);
    Json.Add(TSmSendMstModel.cCdsModelKey).AsString:=string(TSK0001Comm.MemStreamToBase64Str(MemStream));

    ArrayJson:=Json.Add(TSmSendMstModel.cObjItemsKey,jdtArray);
    for Key in fSortKeys do
    begin
      ItemJson:=TQJson.Create;
      if ItemJson.TryParse(Items[Key].SaveToStr()) then
      begin
        ArrayJson.Add(ItemJson);
      end else begin
        ItemJson.Free;
      end;
    end;
    Result:=Json.AsString;
  finally
    Json.Free;
    MemStream.Free;
  end;
end;

function TSmSendMstList.FindByKey(const iKey:string):TSmSendMst;
var
  Item:TSmSendMst;
begin
  if TryGetValue(iKey,Item) then
  begin
    Result:=Item;
  end else begin
    Result:=nil;
  end;
end;

function TSmSendMstList.IsExist(iKey:string):Boolean;
var
  Item:TSmSendMst;
begin
  Result:= TryGetValue(iKey,Item);
end;

function TSmSendMstList.Add(const iItem:TSmSendMst):string;
begin
  if iItem.ObjGUID<>EmptyStr then
  begin
    inherited Add(iItem.ObjGUID,iItem);
    Result:=iItem.ObjGUID;
    fSortKeys.Add(iItem.ObjGUID);
  end else begin
    Result:=EmptyStr;
  end;
end;

function TSmSendMstList.Delete(const iKey:string):Boolean;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Remove(iKey).Free;
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    Result:=True;
  end else begin
    Result:=False;
  end;
end;

function TSmSendMstList.Remove(const iKey:string):TSmSendMst;
var
  Ops:Integer;
begin
  if ContainsKey(iKey) then
  begin
    Result:=Items[iKey];
    Ops:=fSortKeys.IndexOf(iKey);
    if Ops<>-1 then
    begin
      fSortKeys.Delete(Ops);
    end;
    inherited Remove(iKey);
  end else begin
    Result:=nil;
  end;
end;

procedure TSmSendMstList.Clear;
var
  Key:string;
begin
  for Key in Keys do
  begin
    if Items[Key]<>nil then
    begin
      Items[Key].Free;
    end;
  end;
  fSortKeys.Clear;
  inherited Clear;
end;

end.
